<?php
class HandlerModule
{
	public function siteEdit()
	{
		global $_FANWE;
		HandlerModule::siteCheck();
		$site = S('site')->getSiteByUserID($_FANWE['uid']);
		if($site)
		{
			HandlerModule::showSite($site);
		}
	}
	
	public function siteUpdate()
	{
		global $_FANWE;
		HandlerModule::siteCheck();
		
		$id = intval($_FANWE['request']['id']);
		$user_site = S('site')->getSiteByUserID($_FANWE['uid']);
		
		if($user_site['id'] == $id)
		{
			$site['content'] = $_FANWE['request']['content'];
			$user_site['content'] = $site['content'];
			
			include import('class/upload');
			$upload = new Upload();
			$upload->init($_FILES['img_file'],'site');
			if($upload->save())
			{
				if($upload->file['ext'] == 'swf')
					unlink($upload->file['local_target']);
				else
				{
					$site['site_img'] = '/'.$upload->file['target'];
					$user_site['site_img'] = '/'.$upload->file['target'];
				}
			}
			
			S('site')->updateSiteByUserID($id,$_FANWE['uid'],$site);
		}
		else
			exit;
		
		
		HandlerModule::showSite($user_site);
	}
	
	private function showSite($site)
	{
		$site['content'] = stripslashes($site['content']);
		include FANWE_ROOT.'./public/fckeditor/fckeditor.php';
		$fckeditor = new FCKeditor('content');
		$fckeditor->Width = "90%";
		$fckeditor->Height = "300";
		$fckeditor->Value = $site['content'] ;
		$fck = $fckeditor->CreateHtml();
		
		include template('page/site_edit');
		display();
	}
	
	private function siteCheck()
	{
		global $_FANWE;
		if($_FANWE['uid'] < 1 || $_FANWE['group_id'] != SITE_USER_GROUP_ID)
			exit;
	}
	
	public function advAdd()
	{
		global $_FANWE;
		$aid = intval($_FANWE['request']['aid']);
		if($aid == 0)
			exit;
			
		$auction = DB::fetchFirst('SELECT rec_id,rec_data,rec_attr,name FROM '.DB::table('auction').' 
                        WHERE user_id = '.$_FANWE['uid'].' AND id = '.$aid.' AND auction_type = 0');
		
		if(!$auction)
			exit;
		
		$rec_attr = unserialize($auction['rec_attr']);
		$auction['ad_num'] = intval($rec_attr['ad_num']);
		$auction['ad_type'] = $rec_attr['ad_type'];
		unset($auction['rec_attr']);
		
		$ad_num = DB::resultFirst('SELECT count(id) FROM '.DB::table('adv').' WHERE auction_id = '.$aid);
		$is_max = false;
		if($ad_num >= $auction['ad_num'])
		{
			$_FANWE['request']['msg'] = "您已经添加了 $ad_num 条广告";
			$is_max = true;
		}
		
		$adv_types = array(
			'1'=>'图片广告',
			'2'=>'Flash广告',
			'3'=>'自定义代码广告'
		);
		
		if($auction['ad_type'] > 0)
		{
			$adv_types = array($auction['ad_type']=>$adv_types[$auction['ad_type']]);
		}
		
		include template('page/adv_add');
		display();
	}
	
	public function advInsert()
	{
		global $_FANWE;
		$aid = intval($_FANWE['request']['aid']);
		if($aid == 0)
			exit;
			
		$auction = DB::fetchFirst('SELECT rec_id,rec_data,rec_attr,name FROM '.DB::table('auction').' 
                        WHERE user_id = '.$_FANWE['uid'].' AND id = '.$aid.' AND auction_type = 0');
		
		if(!$auction)
			exit;
		
		$rec_attr = unserialize($auction['rec_attr']);
		$auction['ad_num'] = intval($rec_attr['ad_num']);
		$auction['ad_type'] = $rec_attr['ad_type'];
		unset($auction['rec_attr']);
		
		$ad_num = DB::resultFirst('SELECT count(id) FROM '.DB::table('adv').' WHERE auction_id = '.$aid);
		
		if($ad_num >= $auction['ad_num'])
			exit;
			
		$adv['auction_id'] = $aid;
		$adv['position_id'] = $auction['rec_id'];
		$adv['name'] = $_FANWE['request']['adv_name'];
		$adv['type'] = $_FANWE['request']['ad_type'];
		$adv['url'] = $_FANWE['request']['ad_url'];
		$adv['target_key'] = $auction['rec_data'];
		$adv['sort'] = 1;
		$adv['status'] = 1;
		
		if($auction['ad_type'] > 0 && $auction['ad_type'] != $adv['type'])
			exit;
		
		if($adv['type'] == 1 || $adv['type'] = 2)
		{
			include import('class/upload');
			$upload = new Upload();
			$upload->init($_FILES['ad_file'],'adv');
			if($upload->save())
			{
				$adv['code'] = '/'.$upload->file['target'];
				
				if($adv['type'] == 1 && $upload->file['ext'] == 'swf')
				{
					unlink($upload->file['local_target']);
					unset($adv['code']);
				}
				
				if($adv['type'] == 2 && $upload->file['ext'] != 'swf')
				{
					unlink($upload->file['local_target']);
					unset($adv['code']);
				}
			}
		}
		else
			$adv['code'] = '/'.$_FANWE['request']['ad_code'];
		
		DB::insert('adv',$adv);
	
		fHeader('Location: '.U('handler/adv_add',array('aid'=>$aid,'isok'=>1)));
		exit;
	}
	
	public function advEdit()
	{
		global $_FANWE;
		$aid = intval($_FANWE['request']['aid']);
		$id = intval($_FANWE['request']['id']);
		if($aid == 0 || $id == 0)
			exit;
		
		$adv = DB::fetchFirst('SELECT * FROM '.DB::table('adv').' WHERE id = '.$id);
		if(!$adv)
			exit;
		
		$auction = DB::fetchFirst('SELECT rec_id,rec_data,rec_attr,name FROM '.DB::table('auction').' 
					WHERE user_id = '.$_FANWE['uid'].' AND id = '.$aid.' AND auction_type = 0');
       
		if(!$auction || $adv['auction_id'] != $aid)
			exit;
		
		$rec_attr = unserialize($auction['rec_attr']);
		$auction['ad_num'] = intval($rec_attr['ad_num']);
		$auction['ad_type'] = $rec_attr['ad_type'];
		unset($auction['rec_attr']);
		
		$adv_types = array(
			'1'=>'图片广告',
			'2'=>'Flash广告',
			'3'=>'自定义代码广告'
		);
		
		if($auction['ad_type'] > 0)
		{
			$adv_types = array($auction['ad_type']=>$adv_types[$auction['ad_type']]);
		}
		
		include template('page/adv_edit');
		display();
	}
	
	public function advUpdate()
	{
		global $_FANWE;
		$aid = intval($_FANWE['request']['aid']);
		$id = intval($_FANWE['request']['id']);
		
		if($aid == 0 || $id == 0)
			exit;
		
		$adv = DB::fetchFirst('SELECT * FROM '.DB::table('adv').' WHERE id = '.$id);
		if(!$adv)
			exit;
		
		$auction = DB::fetchFirst('SELECT rec_id,rec_data,rec_attr,name FROM '.DB::table('auction').' 
					WHERE user_id = '.$_FANWE['uid'].' AND id = '.$aid.' AND auction_type = 0');
       
		if(!$auction || $adv['auction_id'] != $aid)
			exit;
		
		$adv['name'] = $_FANWE['request']['adv_name'];
		$adv['type'] = $_FANWE['request']['ad_type'];
		$adv['url'] = $_FANWE['request']['ad_url'];
		
		
		if($auction['ad_type'] > 0 && $auction['ad_type'] != $adv['type'])
			exit;
		
		if($adv['type'] == 1 || $adv['type'] == 2)
		{
			include import('class/upload');
			$upload = new Upload();
			$upload->init($_FILES['ad_file'],'adv');
			if($upload->save())
			{
				unlink(FANWE_ROOT.$adv['code']);
				
				$adv['code'] = '/'.$upload->file['target'];
				
				if($adv['type'] == 1 && $upload->file['ext'] == 'swf')
				{
					unlink($upload->file['local_target']);
					unset($adv['code']);
				}
				
				if($adv['type'] == 2 && $upload->file['ext'] != 'swf')
				{
					unlink($upload->file['local_target']);
					unset($adv['code']);
				}
			}
		}
		else
			$adv['code'] = $_FANWE['request']['ad_code'];
		
		DB::update('adv',$adv,'id = '.$id);
		
		fHeader('Location: '.U('handler/adv_edit',array('aid'=>$aid,'id'=>$id,'isok'=>1)));
		exit;
	}
}
?>